# -*- coding: utf-8 -*-
# @time: 2024/6/5 16:06
# @file: index_judge
# @author: tyshixi08

from get_data.origin_data import *
import matplotlib
import matplotlib.pyplot as plt

def drawing_index_judge(df_basic, name):

    '''
    :param df_basic: 指标数据，包括date(字符串)，index_name(浮点数)
    :return df: 回测数据，包括交易日期(字符串)，使用指标(浮点数)，指数净值(浮点数)，卖出滚动{window1}天{int(sell_quantile * 100)}%分位数(浮点数)，买入滚动{window2}天{int(buy_quantile * 100)}%分位数(浮点数)
                            持仓信号(整数型)，持仓状态(整数型)，日收益率(浮点数)，累积净值(浮点数)
    '''
    matplotlib.rcParams['font.sans-serif'] = ['SimHei']
    matplotlib.rcParams['axes.unicode_minus'] = False

    df_basic = df_basic.rename(columns={'date':'交易日期'})
    df_basic_index = basic_index()
    df_basic_index['交易日期'] = pd.to_datetime(df_basic_index['交易日期']).dt.strftime('%Y-%m-%d')
    df = pd.merge(df_basic, df_basic_index, how = 'outer', on = '交易日期').dropna()

    fig, ax1 = plt.subplots(figsize=(10, 6))
    index_plot, = ax1.plot(df['交易日期'], df['指数净值'], color='black', label='基准指数')
    selected_ticks = np.arange(0, len(df), 66)  # 选择每10个点显示一个标签，你可以根据实际情况调整
    plt.xticks(selected_ticks, rotation=45, ha='right')  # rotation旋转角度，ha控制水平对齐方式
    ax2 = ax1.twinx()
    line_plot, = ax2.plot(df['交易日期'], df.iloc[:, 1], color='red', label='使用指标')
    plt.xlabel('时间')
    plt.title(f'{name}')
    plt.legend(handles=[index_plot, line_plot], loc='lower left')
    plt.savefig(f'因子形态判断图/{name}.jpg')
    plt.close()

def massive_drawing():
    ls = ['RSJ_5D', 'RSJ_10D', 'covprice_mid', 'covtend', 'covskew', 'cov_+ret_pct_5D', 'IVdelta_mid_5D',
          'IVdelta_mid_22D', 'YTMdelta_mid', 'covpremium_mid', 'covpremium_modified_mid', 'stock_bondpremium_mid',
          'IVnan_pct']

    for name in ls:
        df = pd.read_csv(f'get_data/原始数据/{name}.csv')
        drawing_index_judge(df, name)